# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

# 配置信息（需修改为实际值）
USERNAME = "zhangjinshan21f@163.com"
PASSWORD = "xy8959929"
# DRIVER_PATH = "/path/to/chromedriver"  # 例如 Windows: r"C:\chromedriver.exe"
LOGIN_URL = "https://w1.v2ai.top/auth/login"


# LOGIN_URL = "https://w1.v2ai.top/user"


def auto_get_vpn_traffic():
    # 初始化浏览器驱动（Chrome示例）
    options = webdriver.ChromeOptions()
    options.add_argument("--disable-infobars")  # 禁用信息栏
    options.add_argument("start-maximized")  # 最大化窗口

    driver = webdriver.Chrome(options=options)
    driver.get(LOGIN_URL)

    try:
        # 等待登录表单加载（根据实际页面调整选择器）
        WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, '#email'))
        )

        # 输入用户名和密码（根据实际页面调整选择器）
        username_field = driver.find_element(By.ID, "email")  # 根据实际name或id修改
        password_field = driver.find_element(By.ID, "passwd")
        #
        username_field.send_keys(USERNAME)
        password_field.send_keys(PASSWORD)

        # 提交登录表单（查找登录按钮）
        login_button = driver.find_element(By.ID, "login")
        login_button.click()

        # 等待登录成功后跳转（根据实际页面调整）
        WebDriverWait(driver, 15).until(
            EC.url_contains("/user")  # 假设登录后跳转到仪表盘
        )

        # 查找并点击领取流量按钮（根据实际按钮文本或选择器调整）
        result_ok_btn = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.ID, "result_ok"))
        )
        result_ok_btn.click()
        print("登录成功....！")

        # 准备活体检测
        doLive(driver)

    except Exception as e:
        print("操作失败:", str(e))
    finally:
        driver.quit()


def doLive(driver):
    checkbox_btn = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "#GurZA4 > div > label > input[type=checkbox]"))
    )

    checkbox_btn.click()
    print("检测是真人了")
    print("检测是真人了")
    print("检测是真人了")
    print("检测是真人了")
    print("检测是真人了")
    print("检测是真人了")


if __name__ == "__main__":
    auto_get_vpn_traffic()
